home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 3792 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.4 KB  |  116 lines

  1. Path: conch.aa.msen.com!casey
  2. From: casey@conch.aa.msen.com (Casey)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: valid date checking routine, HELP !!!!!!!!!!!!
  5. Date: 31 Jan 1996 06:53:25 GMT
  6. Organization: Msen, Inc. -- Ann Arbor, MI.
  7. Message-ID: <4en3l5$2tp@recepsen.aa.msen.com>
  8. References: <DLMMEu.1JI@si.hhs.nl>
  9. NNTP-Posting-Host: conch.aa.msen.com
  10. X-Newsreader: TIN [version 1.2 PL2]
  11.  
  12. /*
  13. **  DAYNUM.C - Functions to return statistics about a given date.
  14. **
  15. **  public domain by Bob Stout - uses Ray Gardner's SCALDATE.C
  16. */
  17.  
  18. int isleap (unsigned yr);
  19. long ymd_to_scalar (unsigned yr, unsigned mo, unsigned day);
  20. static long jan1date;
  21.  
  22. /*
  23. **  Define ISO to be 1 for ISO (Mon-Sun) calendars
  24. **
  25. **  ISO defines the first week with 4 or more days in it to be week #1.
  26. */
  27.  
  28. #ifndef ISO
  29.  #define ISO 0
  30. #endif
  31.  
  32. #if (ISO != 0 && ISO != 1)
  33.  #error ISO must be set to either 0 or 1
  34. #endif
  35.  
  36. /*
  37. **  Determine if a given date is valid
  38. */
  39.  
  40. int valiDate(unsigned yr, unsigned mo, unsigned day)
  41. {
  42.       unsigned int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  43.  
  44.       if (1 > mo || 12 < mo)
  45.             return 0;
  46.       if (1 > day || day > (days[mo - 1] + (2 == mo && isleap(yr))))
  47.             return 0;
  48.       else  return 1;
  49. }
  50.  
  51. /*
  52. **  Return the day of the year (1 - 365/6)
  53. */
  54.  
  55. int daynum(int year, int month, int day)
  56. {
  57.       jan1date = ymd_to_scalar(year, 1, 1);
  58.       return (int)(ymd_to_scalar(year, month, day) - jan1date + 1L);
  59. }
  60.  
  61. /*
  62. **  Return the week of the year (1 - 52)
  63. */
  64.  
  65. int weeknum(int year, int month, int day)
  66. {
  67.       int wn, j1n, dn = daynum(year, month, day);
  68.  
  69.       dn += (j1n = (int)((jan1date - (long)ISO) % 7L)) - 1;
  70.       wn = dn / 7;
  71.       if (ISO)
  72.             wn += (j1n < 4);
  73.       else  ++wn;
  74.       return wn;
  75. }
  76.  
  77. #ifdef TEST
  78.  
  79. #include <stdio.h>
  80. #include <stdlib.h>
  81.  
  82. void do_err(void);
  83.  
  84. void main(int argc, char *argv[])
  85. {
  86.       int day, month, year;
  87.  
  88.       if (4 > argc)
  89.       {
  90.             puts("Usage: DAYNUM month day year");
  91.             return;
  92.       }
  93.  
  94.       month = atoi(argv[1]);
  95.       day   = atoi(argv[2]);
  96.       year  = atoi(argv[3]);
  97.       if (100 > year)
  98.             year += 1900;
  99.  
  100.       printf("%d/%d/%d is day #%d in week %d\n", month, day, year,
  101.             daynum(year, month, day), weeknum(year, month, day));
  102. }
  103.  
  104. #endif /* TEST */
  105.  
  106. Wilde (v922217) wrote:
  107. : Hello,
  108.  
  109. : who can help me with a routine that checks if a date is valid.
  110.  
  111. : Thanks in advance,
  112.  
  113. : Marcel de Wilde
  114. : v922217@si.hhs.nl
  115.  
  116.